<?php
namespace app\admin\controller;
use think\Controller;

class Course extends Controller
{
    public function index()
    {
        $keyword = trim(input('get.keyword'));
        if ($keyword == '' || $keyword == false){
            $course_modules_list = db('coursemodules')->field(['id','course_module'])->paginate(10);
        }else {
            $course_modules_list = db('coursemodules')->field(['id','course_module'])->where('course_module','like','%'.$keyword.'%')->paginate(10);
        }
        $page = $course_modules_list->render();
        $this->assign('course_modules',$course_modules_list);
        $this->assign('page',$page);
        return $this->fetch('index');
    }
    public function save_course_module_view()
    {
        $course_module_id = input('get.course_module_id');
        $keyword = trim(input('get.keyword'));
        if ($course_module_id == ''){
            $this->assign('action','add');
        }else{
            $course_module = db('coursemodules')->where('id',$course_module_id)->find();
            if ($keyword == '' || $keyword == false){
                $course = db('course')->where('parent_id',$course_module_id)->field(['id','course'])->paginate(10,false,['query'=>request()->param()]);
            }else {
                $course = db('course')->where('parent_id',$course_module_id)->where('course','like','%'.$keyword.'%')->field(['id','course'])->paginate(10,false,['query'=>request()->param()]);
            }
            $page = $course->render();
            $this->assign('action','edit');
            $this->assign('course_module',$course_module);
            $this->assign('course',$course);
            $this->assign('page',$page);
        }
        return $this->fetch('course');
    }
    
    public function save_course_module()
    {
        $result = array('err' => '1','content' => '');
        $data = input('post.');
        if ($data['course_module'] == '' || strlen($data['course_module']) > 30){
            $result['content'] = '教程模块名字数请控制在0~30之间！';
            return $result;
        }
        $model = model('Course');
        $save_course_module = $model->saveCourseModule($data);
        if ($save_course_module['res'] == false){
            $result['content'] = '保存课程模块失败！';
            return $result;
        }
        $result['content'] = '绑定模块成功！';
        $result['course_module_id'] = $save_course_module['course_module_id'];
        $result['err'] = '0';
        return $result;
    }
    
    public function save_course_view()
    {
        $course_id = input('get.course_id');
        $course_info = db('course')->field(['id','course','content'])->where('id',$course_id)->find();
        $this->assign('course',$course_info);
        return $this->fetch('save');
    }
    
    public function save_course()
    {
        $result = array('err'=>'1','content'=>'');
        $course_info = input('post.');
        $rules = [
            'id' => 'require|number',
            'course' => 'require|max:50',
        ];
        $message = [
            'id.number' => '数据传输有误！',
            'course.require' =>'教程名必须！',
            'course.max' => '教程名的字数请控制在50内！'
        ];
        $res = $this->validate($course_info, $rules,$message);
        if ($res != true){
            $result['content'] = $res;
            return $result;
        }
        $course_model = model('Course');
        $save_course = $course_model->saveCourse($course_info);
        if ($save_course == false){
            $result['content'] = '保存失败！';
            return $result;
        }
        $result['content'] = '保存成功！';
        $result['err'] = '0';
        return $result;
    }
    
    public function add_course()
    {
        $result = array('err' => '1','content' => '');
        $couser = array();
        $couser['course'] = trim(input('post.course_name'));
        if ($couser['course'] == ''){
            $result['content'] = '教程名不得为空！';
            return $result;
        }
        $couser['created'] = time();
        $couser_model = model('Course');
        $course_id = $couser_model->addCourseName($couser);
        if ($course_id == false){
            $result['content'] = '添加失败！';
            return $result;
        }
        $result['course_id'] = $course_id;
        $result['content'] = '确认成功！';
        $result['err'] = '0';
        return $result;
    }
    public function del_course_module()
    {
        $result = array('err'=>'1','content' => '');
        $course_module_id = input('post.id');
        $del_course_moudule = db('coursemodules')->where('id',$course_module_id)->delete();
        if ($del_course_moudule == false){
            $result['content'] = '删除失败！';
            return $result;
        }
        $result['err'] = '0';
        $result['content'] = '删除成功！';
        return $result;
    }
    public function del_course()
    {
        $result = array('err' => '1','content' => '');
        $course_id = input('post.id');
        $del_course = db('course')->where('id',$course_id)->delete();
        if ($del_course == false){
            $result['content'] = '删除失败！';
            return $result;
        }
        $result['content'] = '删除成功！';
        $result['err'] = '0';
        return $result;
    }

}